package com.hrm.dao.Impl;

import java.util.List;

import com.hrm.dao.UserDao;
import com.hrm.domain.User;
import com.hrm.utils.JDBCTools;

/*
 * 用户数据处理层实现类
 */
@SuppressWarnings("all") // 使用注解抑制警告信息
public class UserDaoImpl implements UserDao {

	// 定义全局变量
	private Class uClass = User.class;

	// 根据账号和密码查找用户
	@Override
	public User getUserByNP(String loginName, String pwd) {
		User user = null;
		String sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where loginname =? and PASSWORD=?;";
		List<User> users = JDBCTools.dataQuery(uClass, sql, loginName, pwd);
		if (!users.isEmpty()) {
			user = users.get(0);
		}
		return user;
	}

	// 根据账号获取指定的用户数据
	@Override
	public User getUserByLN(String loginName) {
		User user = null;
		String sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where loginname = ?;";
		List<User> users = JDBCTools.dataQuery(uClass, sql, loginName);
		if (!users.isEmpty()) {
			user = users.get(0);
		}
		return user;
	}

	// 根据手机号获取指定的用户数据
	@Override
	public User getUserByPhone(String phone) {
		User user = null;
		String sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where phone=?;";
		List<User> users = JDBCTools.dataQuery(uClass, sql, phone);
		if (!users.isEmpty()) {
			user = users.get(0);
		}
		return user;
	}

	// 获取查询结果的记录条数
	@Override
	public Integer getCount(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 两个查询条件都没有输入时
			sql = "select count(*) from user_inf;";
		} else if (flag == 2) { // 只输入了用户名
			sql = "select count(*) from user_inf where username like ?;";
		} else if (flag == 3) { // 只输入了用户状态
			sql = "select count(*) from user_inf where STATUS = ?;";
		} else { // 输入了两个查询条件
			sql = "select count(*) from user_inf where username like ? and STATUS = ?;";
		}
		return JDBCTools.dataCount(sql, parametes);
	}

	// 获取全部用户对象
	@Override
	public List<User> getAllUser() {
		String sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf;";
		return JDBCTools.dataQuery(uClass, sql);
	}

	// 分页查询
	@Override
	public List<User> getUsersByTerm(int flag, Object... parametes) {
		String sql = "";
		if (flag == 1) { // 两个查询条件都没有输入时
			sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf limit ?,?;";
		} else if (flag == 2) { // 只输入了用户名
			sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where username like ? limit ?,?;";
		} else if (flag == 3) { // 只输入了用户状态
			sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where STATUS = ? limit ?,?;";
		} else { // 输入了两个查询条件
			sql = "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where username like ? and STATUS = ? limit ?,?;";
		}
		return JDBCTools.dataQuery(uClass, sql, parametes);
	}

	/*
	 * // 获取当前页的数据
	 * 
	 * @Override public List<User> getUsersByIndexPage(int indexPage, int pageSize)
	 * { String sql =
	 * "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf limit ?,?;"
	 * ; return JDBCTools.dataQuery(uClass, sql, indexPage, pageSize); }
	 * 
	 * // 根据用户名查询当前页数据
	 * 
	 * @Override public List<User> getUsersByUN(String userName, int indexPage, int
	 * pageSize) { String sql =
	 * "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where username = ? limit ?,?;"
	 * ; return JDBCTools.dataQuery(uClass, sql, userName, indexPage, pageSize); }
	 * 
	 * // 根据用户状态获取当前页数据
	 * 
	 * @Override public List<User> getUsersBySta(int status, int indexPage, int
	 * pageSize) { String sql =
	 * "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where STATUS = ? limit ?,?;"
	 * ; return JDBCTools.dataQuery(uClass, sql, status, indexPage, pageSize); }
	 * 
	 * // 根据用户名和用户状态查询当前页数据
	 * 
	 * @Override public List<User> getUsersByUS(String userName, int status, int
	 * indexPage, int pageSize) { String sql =
	 * "select ID uID,loginname uloginName,PASSWORD uPWD,STATUS uStatus,createdate uCreateTime,username uName,phone uPhone from user_inf where username = ? and STATUS = ? limit ?,?;"
	 * ; return JDBCTools.dataQuery(uClass, sql, userName, status, indexPage,
	 * pageSize); }
	 */

	// 添加用户
	@Override
	public Integer addUser(User user) {
		String sql = "INSERT INTO `user_inf` (`loginname`, `PASSWORD`, `STATUS`, `username`, `phone`) VALUES ( ?, ?, 2, ?, ?);";
		return JDBCTools.dataUpdate(sql, user.getUloginName(), user.getuPWD(), user.getuName(), user.getuPhone());
	}

	// 修改用户的全部数据
	@Override
	public Integer modifyUser(User user) {
		String sql = "UPDATE `user_inf` SET `loginname` = ?, `PASSWORD` = ?, `STATUS` = ?, `username` = ?, `phone` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, user.getUloginName(), user.getuPWD(), user.getuStatus(), user.getuName(),
				user.getuPhone(), user.getuID());
	}

	// 根据手机号修改用户密码
	@Override
	public Integer modifyPWDByPhone(String newPWD, String phone) {
		String sql = "UPDATE `user_inf` SET `PASSWORD` = ? WHERE `phone` = ?;";
		return JDBCTools.dataUpdate(sql, newPWD, phone);
	}

	// 根据ID修改用户密码
	@Override
	public Integer modifyPWDByID(int ID, String newPWD) {
		String sql = "UPDATE `user_inf` SET `PASSWORD` = ? WHERE `ID` = ?;";
		return JDBCTools.dataUpdate(sql, newPWD, ID);
	}

	// 根据账号修改用户状态
	@Override
	public Integer modefyStaByLN(int status, String loginName) {
		String sql = "UPDATE `user_inf` SET `STATUS` = ? WHERE `loginname` = ?;";
		return JDBCTools.dataUpdate(sql, status, loginName);
	}

	// 根据用户名删除指定用户
	@Override
	public Integer delUser(String loginName) {
		String sql = "delete from `user_inf` where `loginname` = ?;";
		return JDBCTools.dataUpdate(sql, loginName);
	}

}
